package com.lc.luzhengtang.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lc.luzhengtang.BaseApplication;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.activity.AddressManageActivity;
import com.lc.luzhengtang.activity.ChangeDataActivity;
import com.lc.luzhengtang.activity.CollectionActivity;
import com.lc.luzhengtang.activity.CommenWebActivity;
import com.lc.luzhengtang.activity.CommentListActivity;
import com.lc.luzhengtang.activity.FeedBackActivity;
import com.lc.luzhengtang.activity.LoginActivity;
import com.lc.luzhengtang.activity.MyWalletActivity;
import com.lc.luzhengtang.activity.OrderAllActivity;
import com.lc.luzhengtang.activity.OrderAllActivity2;
import com.lc.luzhengtang.activity.PersonalShoppingActivity;
import com.lc.luzhengtang.activity.ProfitActivity;
import com.lc.luzhengtang.activity.RegisterActivity;
import com.lc.luzhengtang.activity.SettingsActivity;
import com.lc.luzhengtang.activity.TuiGuangActivity;
import com.lc.luzhengtang.adapter.MineListAdapter;
import com.lc.luzhengtang.adapter.MineListAdapter_Quick;
import com.lc.luzhengtang.bean.UserBean;
import com.lc.luzhengtang.conn.Conn;
import com.lc.luzhengtang.conn.ConnPersonalInfo;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;




/**
 * Created by Administrator on 2017/5/2.
 */

public class MineFragment extends BaseFragment3 {
    @BindView(R.id.img_settings)
    ImageView imgSettings;
    @BindView(R.id.iv_search)
    ImageView ivSearch;
    @BindView(R.id.text_normal_person)
    TextView textNormalPerson;
    @BindView(R.id.img_wenhao)
    ImageView imgWenhao;
    @BindView(R.id.layout_putong)
    LinearLayout layoutPutong;
    @BindView(R.id.mine_head_iv)
    SimpleDraweeView mineHeadIv;
    @BindView(R.id.img_arrow_right)
    ImageView imgArrowRight;
    @BindView(R.id.layout_jump_changeData)
    RelativeLayout layoutJumpChangeData;
    @BindView(R.id.text_register)
    TextView textRegister;
    @BindView(R.id.text_login)
    TextView textLogin;
    @BindView(R.id.layout_register_login)
    LinearLayout layoutRegisterLogin;
    @BindView(R.id.text_username)
    TextView textUsername;
    @BindView(R.id.text_name)
    TextView textName;
    @BindView(R.id.layout_nicheng)
    LinearLayout layoutNicheng;
    @BindView(R.id.img_dingdan)
    ImageView imgDingdan;
    @BindView(R.id.search_all_payorder)
    RelativeLayout searchAllPayorder;
    @BindView(R.id.text_daifukuan)
    ImageView textDaifukuan;
    @BindView(R.id.layout_title)
    RelativeLayout layoutTitle;
    @BindView(R.id.text_daifu)
    TextView textDaifu;
    @BindView(R.id.layout_daifu)
    RelativeLayout layoutDaifu;
    @BindView(R.id.layout_daifu_rel)
    RelativeLayout layoutDaifuRel;
    @BindView(R.id.text_daifa)
    TextView textDaifa;
    @BindView(R.id.layout_daifa)
    RelativeLayout layoutDaifa;
    @BindView(R.id.layout_daifu_rel2)
    RelativeLayout layoutDaifuRel2;
    @BindView(R.id.text_daishou)
    TextView textDaishou;
    @BindView(R.id.layout_daishou)
    RelativeLayout layoutDaishou;
    @BindView(R.id.layout_daifu_rell)
    RelativeLayout layoutDaifuRell;
    @BindView(R.id.text_daicomment)
    TextView textDaicomment;
    @BindView(R.id.layout_daipingjia)
    RelativeLayout layoutDaipingjia;
    @BindView(R.id.layout_shouhuo)
    RelativeLayout layoutShouhuo;
    @BindView(R.id.text_tuikuan)
    TextView textTuikuan;
    @BindView(R.id.layout_daishouhuo)
    RelativeLayout layoutDaishouhuo;
    @BindView(R.id.img_shoucang)
    ImageView imgShoucang;
    @BindView(R.id.layout_shoucang)
    RelativeLayout layoutShoucang;
    @BindView(R.id.img_dianpu)
    ImageView imgDianpu;
    @BindView(R.id.layout_dianpu)
    RelativeLayout layoutDianpu;
    @BindView(R.id.img_shouyi)
    ImageView imgShouyi;
    @BindView(R.id.layout_shouyi)
    RelativeLayout layoutShouyi;
    @BindView(R.id.img_qianbao)
    ImageView imgQianbao;
    @BindView(R.id.text_yue)
    TextView textYue;
    @BindView(R.id.text_weijiesuan)
    TextView textWeijiesuan;
    @BindView(R.id.layout_yue)
    LinearLayout layoutYue;
    @BindView(R.id.layout_qianbao)
    RelativeLayout layoutQianbao;
    @BindView(R.id.img_tuiguagnma)
    ImageView imgTuiguagnma;
    @BindView(R.id.layout_tuiguangma)
    RelativeLayout layoutTuiguangma;
    @BindView(R.id.img_yijian)
    ImageView imgYijian;
    @BindView(R.id.layout_feedback)
    RelativeLayout layoutFeedback;
    @BindView(R.id.img_address)
    ImageView imgAddress;
    @BindView(R.id.layout_address_managment)
    RelativeLayout layoutAddressManagment;
    @BindView(R.id.img_pingjia)
    ImageView imgPingjia;
    @BindView(R.id.layout_pingjia)
    RelativeLayout layoutPingjia;
    Unbinder unbinder;

    MineListAdapter mineListAdapter;

    List<String> stringList = new ArrayList<>();
    @BindView(R.id.rv_list)
    RecyclerView rvList;


    MineListAdapter_Quick mineListAdapter_quick;


    {
        int i = 0;
        while (++i <= 5)
            stringList.add("0");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.fragment_mine, null);
        unbinder = ButterKnife.bind(this, view);
        mineListAdapter = new MineListAdapter(getActivity(), stringList);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);


        rvList.setLayoutManager(linearLayoutManager);
        rvList.setAdapter(mineListAdapter);
        return view;
    }

    public void refresh() {
        stringList.clear();
        int i = 0;
        while (++i <= 5)
            stringList.add("0");

        mineListAdapter.notifyDataSetChanged();

    }

    @Override
    public void onResume() {
        super.onResume();
        if (TextUtils.isEmpty(BaseApplication.basePreferences.readUid())) {
            layoutRegisterLogin.setVisibility(View.VISIBLE);
            textUsername.setVisibility(View.GONE);
            textName.setVisibility(View.GONE);
            layoutNicheng.setVisibility(View.GONE);
            layoutPutong.setVisibility(View.GONE);
            imgArrowRight.setVisibility(View.GONE);
//            mine_head_iv
            mineHeadIv.setImageResource(R.mipmap.touxiang);
            layoutYue.setVisibility(View.GONE);
            textDaifa.setVisibility(View.GONE);
            textDaifu.setVisibility(View.GONE);
            textDaishou.setVisibility(View.GONE);
            textDaicomment.setVisibility(View.GONE);
            textTuikuan.setVisibility(View.GONE);

//            stringList.clear();
//            int i = 0;
//            while (++i <= 5)
//                stringList.add("0");
//
//            mineListAdapter.notifyDataSetChanged();

        } else {
            layoutNicheng.setVisibility(View.VISIBLE);

            layoutRegisterLogin.setVisibility(View.GONE);
            textUsername.setVisibility(View.VISIBLE);
            textName.setVisibility(View.VISIBLE);
            layoutPutong.setVisibility(View.VISIBLE);
            imgArrowRight.setVisibility(View.VISIBLE);
//
            textDaifa.setVisibility(View.VISIBLE);
            textDaifu.setVisibility(View.VISIBLE);
            textDaishou.setVisibility(View.VISIBLE);
            textDaicomment.setVisibility(View.VISIBLE);
            textTuikuan.setVisibility(View.VISIBLE);
            getUserData();
        }

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);

    }

    public void getUserData() {
        Log.d(BaseApplication.basePreferences.readUid(), "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq");
        Log.d(BaseApplication.basePreferences.readUid(), "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq");
        new ConnPersonalInfo(BaseApplication.basePreferences.readUid(), new AsyCallBack<UserBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
                showLoading();
            }

            @Override
            public void onSuccess(String toast, int type, Object o, UserBean userBean) throws Exception {
                super.onSuccess(toast, type, o, userBean);

                textUsername.setText(userBean.getData().getUsername());
//                text_normal_person
                textNormalPerson.setText(userBean.getData().getRole_id());
//                textUsername.setText(userBean.getData().getUsername());
                if (TextUtils.isEmpty(userBean.getData().getShangjiname())) {
                    textName.setText(userBean.getData().getShangjiname());
                } else {
                    textName.setText("我的上级:" + userBean.getData().getShangjiname());
                }


//                Uri.parse(userBean.getData().getAvatar())
                mineHeadIv.setImageURI(Conn.HOST3 + userBean.getData().getAvatar());
//                textUsername.setText(userBean.getData().getUsername());
//                textName.setText(userBean.getData().getName());
//                mineHeadIv.setImageURI(Conn.HOST3 + Uri.parse(userBean.getData().getAvatar()));
//                mineHeadIv.setImageURI(Conn.HOST2 + BaseApplication.basePreferences.readAvatar());
                textYue.setText("￥" + userBean.getData().getBalance() + "");
                textWeijiesuan.setText("￥" + userBean.getData().getTaskinfo() + "");
                BaseApplication.basePreferences.saveAvatar(Conn.HOST3 + userBean.getData().getAvatar());
                BaseApplication.basePreferences.saveUserName(userBean.getData().getUsername());
                BaseApplication.basePreferences.saveMobile(userBean.getData().getMobile());
                BaseApplication.basePreferences.savePhone(userBean.getData().getPhone());

                BaseApplication.basePreferences.saveName(userBean.getData().getName());

                BaseApplication.basePreferences.saveSex(userBean.getData().getSex());

                BaseApplication.basePreferences.saveEmail(userBean.getData().getEmail());
                BaseApplication.basePreferences.saveQq(userBean.getData().getQq());
                BaseApplication.basePreferences.saveOpenId(userBean.getData().getOpen_id());
//                BaseApplication.basePreferences.saveTuijian(userBean.getData().get);
                BaseApplication.basePreferences.saveBalance(userBean.getData().getBalance());

                open_id = userBean.getData().getOpen_id();
                stringList.clear();
                for (int i = 0; i < userBean.getNumnum().size(); i++) {
                    stringList.add(userBean.getNumnum().get(i).getNumber());
                }

                mineListAdapter.notifyDataSetChanged();


                mineListAdapter.setOnItemClickListener(new MineListAdapter.MyOnItemClickListener() {
                    @Override
                    public void OnItemClickListener(View view, int position) {
                    /*    Intent intent2 = new Intent();
                        intent2.putExtra("ids", position + "");
                        intent2.setClass(getActivity(), OrderAllActivity.class);
                        startActivity(intent2);*/
                        if (position == 0) {
                            startActivity(new Intent(getActivity(), OrderAllActivity.class));
//                    SimpleOrderFragment.getInstance("3");
                        } else if (position == 1) {
                            Intent intent6 = new Intent();
                            intent6.putExtra("ids", "2");
                            intent6.setClass(getActivity(), OrderAllActivity.class);
                            startActivity(intent6);
                        } else if (position == 2) {
                            Intent intent8 = new Intent();
                            intent8.putExtra("ids", "3");
                            intent8.setClass(getActivity(), OrderAllActivity.class);
                            startActivity(intent8);
                        } else if (position == 3) {
                            Intent intent4 = new Intent();
                            intent4.putExtra("ids", "4");
                            intent4.setClass(getActivity(), OrderAllActivity.class);
                            startActivity(intent4);
                        } else if (position == 4) {
                            Intent intent4 = new Intent();
                            intent4.putExtra("ids", "5");
                            intent4.setClass(getActivity(), OrderAllActivity.class);
                            startActivity(intent4);
                        } else {
                            Intent intent5 = new Intent();
                            intent5.putExtra("ids", "3");
                            intent5.setClass(getActivity(), OrderAllActivity.class);
                            startActivity(intent5);
                        }
                    }
                });

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                dismissLoading();
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                dismissLoading();
            }
        }
        ).execute(getActivity(), false);
    }

    String open_id;

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.img_settings, R.id.iv_search, R.id.mine_head_iv, R.id.text_register, R.id.text_login, R.id.img_dingdan, R.id.search_all_payorder, R.id.layout_daifu, R.id.layout_daifa, R.id.layout_daishou, R.id.layout_daipingjia, R.id.layout_daishouhuo, R.id.img_shoucang, R.id.layout_shoucang, R.id.img_dianpu, R.id.layout_dianpu, R.id.img_shouyi, R.id.layout_shouyi, R.id.img_qianbao, R.id.layout_qianbao, R.id.img_tuiguagnma, R.id.layout_tuiguangma, R.id.img_yijian, R.id.layout_feedback, R.id.img_address, R.id.layout_address_managment, R.id.img_pingjia, R.id.layout_pingjia, R.id.text_normal_person, R.id.img_wenhao, R.id.layout_putong, R.id.img_arrow_right, R.id.layout_jump_changeData})
    public void onViewClicked(View view) {
        if (BaseApplication.basePreferences.readUid().isEmpty() && view.getId() == R.id.text_register) {
            startActivity(new Intent(getActivity(), RegisterActivity.class));
        } else if (BaseApplication.basePreferences.readUid().isEmpty()) {
            startActivity(new Intent(getActivity(), LoginActivity.class));
        } else {
            switch (view.getId()) {
                case R.id.img_settings:
                    startActivity(new Intent(getActivity(), SettingsActivity.class));
                    break;
                case R.id.iv_search:
                    break;
                case  R.id.mine_head_iv:
//                    startActivity(new Intent(getActivity(), ChangeDataActivity.class));
                    Intent intent = new Intent();
                    intent.putExtra("open_id", open_id);


                    intent.setClass(getActivity(), ChangeDataActivity.class);

                    startActivity(intent);
                    break;
                case R.id.text_register:


                    break;
                case R.id.text_login:
                    //跳转到登陆界面
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                    break;
                case R.id.img_dingdan:
                    break;
                case R.id.search_all_payorder:
                    //查看全部订单
                    startActivity(new Intent(getActivity(), OrderAllActivity2.class));
                    break;
                case R.id.layout_daifu:
                    startActivity(new Intent(getActivity(), OrderAllActivity.class));
//                    SimpleOrderFragment.getInstance("2");
                    break;
                case R.id.layout_daifa:
//                    startActivity(new Intent(getActivity(), OrderAllActivity.class));
//                    SimpleOrderFragment.getInstance("3");
                    Intent intent2 = new Intent();
                    intent2.putExtra("ids", "2");
                    intent2.setClass(getActivity(), OrderAllActivity.class);
                    startActivity(intent2);
                    break;
                case R.id.layout_daishou:
//                    startActivity(new Intent(getActivity(), OrderAllActivity.class));
                    Intent intent3 = new Intent();
                    intent3.putExtra("ids", "3");
                    intent3.setClass(getActivity(), OrderAllActivity.class);
                    startActivity(intent3);
                    break;
                case R.id.layout_daipingjia:
//                    startActivity(new Intent(getActivity(), OrderAllActivity.class));
                    Intent intent4 = new Intent();
                    intent4.putExtra("ids", "4");
                    intent4.setClass(getActivity(), OrderAllActivity.class);
                    startActivity(intent4);
                    break;
                case R.id.layout_daishouhuo:
                    Intent intent5 = new Intent();
                    intent5.putExtra("ids", "5");
                    intent5.setClass(getActivity(), OrderAllActivity.class);
                    startActivity(intent5);
                    break;
                case R.id.img_shoucang:
                    break;
                case R.id.layout_shoucang:
                    startActivity(new Intent(getActivity(), CollectionActivity.class));

                    break;
                case R.id.img_dianpu:
                    break;
                case R.id.layout_dianpu:
                    startActivity(new Intent(getActivity(), PersonalShoppingActivity.class));
                    break;
                case R.id.img_shouyi:
                    break;
                case R.id.layout_shouyi:
                    startActivity(new Intent(getActivity(), ProfitActivity.class));

                    break;
                case R.id.img_qianbao:
                    break;
                case R.id.layout_qianbao:
                    startActivity(new Intent(getActivity(), MyWalletActivity.class));
                    break;
                case R.id.img_tuiguagnma:
                    break;
                case R.id.layout_tuiguangma:
                    startActivity(new Intent(getActivity(), TuiGuangActivity.class));
                    break;
                case R.id.img_yijian:
                    break;
                case R.id.layout_feedback:
                    startActivity(new Intent(getActivity(), FeedBackActivity.class));
                    break;
                case R.id.img_address:
                    break;
                case R.id.layout_address_managment:
                    startActivity(new Intent(getActivity(), AddressManageActivity.class));
                    break;
                case R.id.img_pingjia:
                    break;
                case R.id.layout_pingjia:
                    startActivity(new Intent(getActivity(), CommentListActivity.class));
                    //   startActivity(new Intent(getActivity(), SectionUseActivity.class));
                    break;

                case R.id.text_normal_person:
                    break;
                case R.id.img_wenhao:
                    Intent intent9 = new Intent();

                    intent9.putExtra("url", Conn.HOST2 + "index.php/api/user/MyPage?id=5");
                    intent9.putExtra("flags", "1");
                    intent9.setClass(getActivity(), CommenWebActivity.class);
                    startActivity(intent9);

                    break;
                case R.id.layout_putong:
                    break;

                case R.id.img_arrow_right:
//                    startActivity(new Intent(getActivity(), ChangeDataActivity.class));
                    Intent intent12 = new Intent();
                    intent12.putExtra("open_id", open_id);


                    intent12.setClass(getActivity(), ChangeDataActivity.class);

                    startActivity(intent12);
                    break;

                case R.id.layout_jump_changeData:
//                    startActivity(new Intent(getActivity(), ChangeDataActivity.class));
                    Intent intent10 = new Intent();
                    intent10.putExtra("open_id", open_id);


                    intent10.setClass(getActivity(), ChangeDataActivity.class);

                    startActivity(intent10);
                    break;
            }
        }


    }


    @Override
    protected void initData() {

    }


}
